/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
typedef StaticTask_t osStaticThreadDef_t;
typedef StaticQueue_t osStaticMessageQDef_t;
typedef StaticEventGroup_t osStaticEventGroupDef_t;
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for sysTask */
osThreadId_t sysTaskHandle;
uint32_t sysTaskBuffer[ 512 ];
osStaticThreadDef_t sysTaskControlBlock;
const osThreadAttr_t sysTask_attributes = {
  .name = "sysTask",
  .cb_mem = &sysTaskControlBlock,
  .cb_size = sizeof(sysTaskControlBlock),
  .stack_mem = &sysTaskBuffer[0],
  .stack_size = sizeof(sysTaskBuffer),
  .priority = (osPriority_t) osPriorityHigh,
};
/* Definitions for appTopic */
osThreadId_t appTopicHandle;
uint32_t appTopicBuffer[ 128 ];
osStaticThreadDef_t appTopicControlBlock;
const osThreadAttr_t appTopic_attributes = {
  .name = "appTopic",
  .cb_mem = &appTopicControlBlock,
  .cb_size = sizeof(appTopicControlBlock),
  .stack_mem = &appTopicBuffer[0],
  .stack_size = sizeof(appTopicBuffer),
  .priority = (osPriority_t) osPriorityAboveNormal,
};
/* Definitions for appTask */
osThreadId_t appTaskHandle;
uint32_t appTaskBuffer[ 128 ];
osStaticThreadDef_t appTaskControlBlock;
const osThreadAttr_t appTask_attributes = {
  .name = "appTask",
  .cb_mem = &appTaskControlBlock,
  .cb_size = sizeof(appTaskControlBlock),
  .stack_mem = &appTaskBuffer[0],
  .stack_size = sizeof(appTaskBuffer),
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for sysNotify */
osThreadId_t sysNotifyHandle;
uint32_t sysNotifyBuffer[ 512 ];
osStaticThreadDef_t sysNotifyControlBlock;
const osThreadAttr_t sysNotify_attributes = {
  .name = "sysNotify",
  .cb_mem = &sysNotifyControlBlock,
  .cb_size = sizeof(sysNotifyControlBlock),
  .stack_mem = &sysNotifyBuffer[0],
  .stack_size = sizeof(sysNotifyBuffer),
  .priority = (osPriority_t) osPriorityBelowNormal,
};
/* Definitions for servoReply */
osMessageQueueId_t servoReplyHandle;
uint8_t servoReplyBuffer[ 16 * sizeof( uint32_t ) ];
osStaticMessageQDef_t servoReplyControlBlock;
const osMessageQueueAttr_t servoReply_attributes = {
  .name = "servoReply",
  .cb_mem = &servoReplyControlBlock,
  .cb_size = sizeof(servoReplyControlBlock),
  .mq_mem = &servoReplyBuffer,
  .mq_size = sizeof(servoReplyBuffer)
};
/* Definitions for sbus_data_ready_event_ */
osEventFlagsId_t sbus_data_ready_event_Handle;
osStaticEventGroupDef_t sbus_data_ready_event_ControlBlock;
const osEventFlagsAttr_t sbus_data_ready_event__attributes = {
  .name = "sbus_data_ready_event_",
  .cb_mem = &sbus_data_ready_event_ControlBlock,
  .cb_size = sizeof(sbus_data_ready_event_ControlBlock),
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void sys_task(void *argument);
void app_topic(void *argument);
void app_task(void *argument);
void sys_notify(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* Hook prototypes */
void vApplicationIdleHook(void);
void vApplicationTickHook(void);

/* USER CODE BEGIN 2 */
void vApplicationIdleHook( void )
{
   /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
   to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
   task. It is essential that code added to this hook function never attempts
   to block in any way (for example, call xQueueReceive() with a block time
   specified, or call vTaskDelay()). If the application makes use of the
   vTaskDelete() API function (as this demo application does) then it is also
   important that vApplicationIdleHook() is permitted to return to its calling
   function, because it is the responsibility of the idle task to clean up
   memory allocated by the kernel to any task that has since been deleted. */
}
/* USER CODE END 2 */

/* USER CODE BEGIN 3 */
void vApplicationTickHook( void )
{
   /* This function will be called by each tick interrupt if
   configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h. User code can be
   added here, but the tick hook is called from an interrupt context, so
   code must not attempt to block, and only the interrupt safe FreeRTOS API
   functions can be used (those that end in FromISR()). */
}
/* USER CODE END 3 */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* creation of servoReply */
  servoReplyHandle = osMessageQueueNew (16, sizeof(uint32_t), &servoReply_attributes);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of sysTask */
  sysTaskHandle = osThreadNew(sys_task, NULL, &sysTask_attributes);

  /* creation of appTopic */
  appTopicHandle = osThreadNew(app_topic, NULL, &appTopic_attributes);

  /* creation of appTask */
  appTaskHandle = osThreadNew(app_task, NULL, &appTask_attributes);

  /* creation of sysNotify */
  sysNotifyHandle = osThreadNew(sys_notify, NULL, &sysNotify_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* Create the event(s) */
  /* creation of sbus_data_ready_event_ */
  sbus_data_ready_event_Handle = osEventFlagsNew(&sbus_data_ready_event__attributes);

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_sys_task */
/**
  * @brief  Function implementing the sysTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_sys_task */
__weak void sys_task(void *argument)
{
  /* USER CODE BEGIN sys_task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END sys_task */
}

/* USER CODE BEGIN Header_app_topic */
/**
* @brief Function implementing the appTopic thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_app_topic */
__weak void app_topic(void *argument)
{
  /* USER CODE BEGIN app_topic */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END app_topic */
}

/* USER CODE BEGIN Header_app_task */
/**
* @brief Function implementing the appTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_app_task */
__weak void app_task(void *argument)
{
  /* USER CODE BEGIN app_task */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END app_task */
}

/* USER CODE BEGIN Header_sys_notify */
/**
* @brief Function implementing the sysNotify thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_sys_notify */
__weak void sys_notify(void *argument)
{
  /* USER CODE BEGIN sys_notify */
  /* Infinite loop */
  for(;;)
  {
    osDelay(100);
  }
  /* USER CODE END sys_notify */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

